package br.com.vti.sisp_sicrim.antigo.dao;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.UUID;

import br.com.vti.sisp_sicrim.antigo.dao.util.HibernateUtil;

public class SicOcorrenciasDAO {

	private int codigoRetorno = 0; // *ERROR
	private String msgRetorno = ""; // *ERROR
	public long qtdRegistros = 0; // *counter
	public long nroRegistro = 0; // *counter
	private String[] listacol = { "", "RegEstadualGeralOcorr", "ArtigoOcorr", "IncisoOcorr", "AlineaOcorr", "ParagrafoOcorr", "LeiOcorr", "AnoLeiOcorr" };
	private ResultSet query = null;
	private ResultSet queryIncidenciasCriminaisOcorr = null;

	public String insert(Long pRegEstadualGeralOcorr, Integer pArtigoOcorr, String pIncisoOcorr, String pAlineaOcorr, Integer pParagrafoOcorr, Integer pLeiOcorr, Integer pAnoLeiOcorr) {

		String sql = "{ ? = call sp_grava_Sic_Ocorrencias ( ?,?,?,?,?,?,? ) }";
		UUID pId;
		codigoRetorno = 0;
		qtdRegistros = 0;
		nroRegistro = 0;
		msgRetorno = "";
		try {

			CallableStatement stp = HibernateUtil.getConnection().prepareCall(sql);
			stp.registerOutParameter(1, Types.OTHER);
			stp.setLong(2, pRegEstadualGeralOcorr); // (getColuna("RegEstadualGeralOcorr")
			stp.setInt(3, pArtigoOcorr); // (getColuna("ArtigoOcorr")
			stp.setString(4, pIncisoOcorr); // (getColuna("IncisoOcorr")
			stp.setString(5, pAlineaOcorr); // (getColuna("AlineaOcorr")
			stp.setInt(6, pParagrafoOcorr); // (getColuna("ParagrafoOcorr")
			stp.setInt(7, pLeiOcorr); // (getColuna("LeiOcorr")
			stp.setInt(8, pAnoLeiOcorr); // (getColuna("AnoLeiOcorr")
			// execute SP
			stp.execute();
			pId = (UUID) stp.getObject(1);
		} catch (SQLException e) {
			codigoRetorno = 999;
			msgRetorno = e.getMessage();
			return null;
		}
		return String.valueOf(pId);
	}

	public void insertIncidenciasCriminaisOcorr(String pId, int pSeq, String pIncidenciasCriminaisOcorr) {
		String sql = "{ call sp_grava_Sic_OcorrenciasIncidenciasCriminaisOcorr ( ?, ?, ? ) }";
		try {

			CallableStatement stp = HibernateUtil.getConnection().prepareCall(sql);
			stp.setObject(1, UUID.fromString(pId)); // getColuna("Id")
			stp.setInt(2, pSeq); // getColuna("Seq")
			stp.setString(3, pIncidenciasCriminaisOcorr); // getColuna("IncidenciasCriminaisOcorr")
			stp.execute();
		} catch (SQLException e) {
			codigoRetorno = 999;
			msgRetorno = e.getMessage();
		}

	}

	public void update(String pId, Long pRegEstadualGeralOcorr, Integer pArtigoOcorr, String pIncisoOcorr, String pAlineaOcorr, Integer pParagrafoOcorr, Integer pLeiOcorr, Integer pAnoLeiOcorr) {

		String sql = "{ call sp_atualiza_Sic_Ocorrencias ( ?,?,?,?,?,?,?,? ) }";
		codigoRetorno = 0;
		qtdRegistros = 0;
		nroRegistro = 0;
		msgRetorno = "";
		try {

			CallableStatement stp = HibernateUtil.getConnection().prepareCall(sql);
			stp.setObject(1, UUID.fromString(pId)); // getColuna("Id")
			stp.setLong(2, pRegEstadualGeralOcorr); // getColuna("RegEstadualGeralOcorr")
			stp.setInt(3, pArtigoOcorr); // getColuna("ArtigoOcorr")
			stp.setString(4, pIncisoOcorr); // getColuna("IncisoOcorr")
			stp.setString(5, pAlineaOcorr); // getColuna("AlineaOcorr")
			stp.setInt(6, pParagrafoOcorr); // getColuna("ParagrafoOcorr")
			stp.setInt(7, pLeiOcorr); // getColuna("LeiOcorr")
			stp.setInt(8, pAnoLeiOcorr); // getColuna("AnoLeiOcorr")
			// execute SP
			stp.execute();
		} catch (SQLException e) {
			codigoRetorno = 999;
			msgRetorno = e.getMessage();
		}
	}

	public void updateIncidenciasCriminaisOcorr(String pId, int pSeq, String pIncidenciasCriminaisOcorr) {
		String sql = "{ call sp_atualiza_Sic_OcorrenciasIncidenciasCriminaisOcorr ( ?, ?, ? ) }";
		try {

			CallableStatement stp = HibernateUtil.getConnection().prepareCall(sql);
			stp.setObject(1, UUID.fromString(pId)); // getColuna("Id")
			stp.setInt(2, pSeq); // getColuna("Seq")
			stp.setString(3, pIncidenciasCriminaisOcorr); // getColuna("IncidenciasCriminaisOcorr")
			stp.execute();
		} catch (SQLException e) {
			codigoRetorno = 999;
			msgRetorno = e.getMessage();
		}

	}

	public ResultSet findRegEstadualGeralOcorr(Long pRegEstadualGeralOcorr) {

		String sql = "select * from sp_pesqRegEstadualGeralOcorr_Sic_Ocorrencias (";
		sql = sql + "pRegEstadualGeralOcorr := '" + String.valueOf(pRegEstadualGeralOcorr) + "' ";
		sql = sql + ")";
		codigoRetorno = 0;
		qtdRegistros = 0;
		nroRegistro = 0;
		msgRetorno = "";
		try {
			query = HibernateUtil.executarQuery(sql);
			if (query == null) {
				return null;
			} else {
				query.first();
				if (query.getRow() == 0) {
					query = null;
					return query;
				}
				if (query.first()) {
					query.last();
					qtdRegistros = query.getRow();
					nroRegistro = 1;
					query.first();
				}
			}
		} catch (SQLException e) {
			codigoRetorno = 999;
			msgRetorno = e.getMessage();
		}

		return query;
	}

	public ResultSet pesquisaProximo() {
		// query
		try {
			if (query == null)
				return null;
			if (query.getRow() == 0) {
				query = null;
				return null;
			}
			query.next();
			if (query.getRow() == 0) {
				query = null;
				return null;
			}
			if (query != null) {
				nroRegistro++;
			}
			return query;
		} catch (SQLException e) {
			codigoRetorno = 999;
			msgRetorno = e.getMessage();
			return null;
		}
	}

	public boolean temProximo() {
		try {
			if (query == null)
				return false;
			if (query.getRow() == 0) {
				return false;
			}
		} catch (SQLException e) {
			codigoRetorno = 999;
			msgRetorno = e.getMessage();
			return false;
		}
		return (nroRegistro < qtdRegistros);
	}

	public void fecha() {
		if (query == null)
			return;
		try {
			query.close();
			query = null;
		} catch (SQLException e) {
			codigoRetorno = 999;
			msgRetorno = e.getMessage();
			query = null;
		}
	}

	public ResultSet pesquisaIncidenciasCriminaisOcorr(String id) {

		String sql = "select * from sp_pesquisa_IncidenciasCriminaisOcorr_Sic_Ocorrencias ( pId: = '" + id + "' )";
		try {
			queryIncidenciasCriminaisOcorr = HibernateUtil.executarQuery(sql);
			queryIncidenciasCriminaisOcorr.first();
		} catch (SQLException e) {
			codigoRetorno = 999;
			msgRetorno = e.getMessage();
		}

		return queryIncidenciasCriminaisOcorr;
	}

	public ResultSet proximoIncidenciasCriminaisOcorr() {

		if (queryIncidenciasCriminaisOcorr == null) {
			return null;
		}
		try {
			queryIncidenciasCriminaisOcorr.next();
		} catch (SQLException e) {
			codigoRetorno = 999;
			msgRetorno = e.getMessage();
			return null;
		}

		return queryIncidenciasCriminaisOcorr;
	}

	public int getColuna(String coluna) {
		for (int i = 0; i < listacol.length; i++) {
			if (listacol[i].equals(coluna))
				return i;
		}
		return -1;
	}

}
